package GUI;
import Control.Episode;


public class ScrollBarHorizontal {

	int swidth, sheight;    // width and height of bar
	int xpos, ypos;         // x and y position of bar
	float spos1, newspos1, spos2, newspos2;    // x position of slider
	int sposMin1, sposMax1,sposMin2, sposMax2;   // max and min values of slider
	int loose;              // how loose/heavy
	boolean over1;           // is the mouse over the first slider?
	boolean over2;           // is the mouse over the second slider?
	boolean locked1,locked2;
	float ratio;
	int scale;
	int val;
	public boolean visible;
	public float newJslider1=0,newJslider2=0;
	private Episode lastEpisode;
	boolean slider1Changed;
	boolean slider2Changed;
	

	////////////////////////////////////////////////////////////////////////////////////////////////////
	ScrollBarHorizontal (int xp, int yp, int sw, int sh, int l) {
		swidth = sw;
		sheight = sh;
		int widthtoheight = sw - sh;
		ratio = (float)sw / (float)widthtoheight;
		xpos = xp;
		ypos = yp-sheight/2;
		spos1 = xpos;
		newspos1 = spos1;
		sposMin1 = xpos;
		sposMax1 = xpos;

		spos2 = xpos + swidth - sheight+1;
		newspos2 = spos2;
		sposMin2 = xpos + swidth - sheight +1;
		sposMax2 = xpos + swidth - sheight +1;
		scale =Utils.scaleEpisode-1; //default scale season
		loose = l;
		visible=false;
		lastEpisode = Utils.startEpisodeSelected;
	}


	void update() {
		if(over1()) {
			over1 = true;
		} else {
			over1 = false;
		}
		if(over2()) {
			over2 = true;
		} else {
			over2 = false;
		}
		if(Utils.globalProcessing.mousePressed && over1 && !locked2) {
			locked1 = true;
		}
		else if(Utils.globalProcessing.mousePressed && over2 &&!locked1) {
			locked2 = true;
		}
		if(!Utils.globalProcessing.mousePressed) {
			locked1 = false;
			locked2 = false;
		}
		if(locked1) {
			newspos1 = constrain(Utils.globalProcessing.mouseX-sheight/2, sposMin1, sposMin2);
			float val =((newspos1-xpos)*scale/(swidth));
			newspos1 = (int)(val+0.5) * (swidth)/scale + xpos;
			//System.out.println(newspos1);
			//newspos1 = ((newspos1-sposMin1)*scale/(sposMax2-sposMin1))+sposMin1;
			newspos1 = constrain(Utils.globalProcessing.mouseX-sheight/2, sposMin1, sposMin2);
			sposMax1= (int)(newspos1);

			//System.out.println("newspos1 = "+newspos1+"   sposMax1 = "+sposMax1);

		}
		else if(locked2) {
			newspos2 = constrain(Utils.globalProcessing.mouseX-sheight/2, sposMax1, sposMax2);
			//System.out.println("newspos2 (before) = "+newspos2);
			float val =((newspos2-xpos)*scale/(swidth));
			//System.out.println("val "+val);
			newspos2 = (int)(val+0.5) * (swidth)/scale + xpos;
			newspos2 = constrain(Utils.globalProcessing.mouseX-sheight/2, sposMax1, sposMax2);
			//System.out.println("newspos2 = "+newspos2+"  sposMax1 = "+sposMax1);
			sposMin2 =(int)(newspos2);
		}
		if(Utils.globalProcessing.abs(newspos1 - spos1) > 1) {
			spos1 = spos1 + (newspos1-spos1)/loose;
		}
		if(Utils.globalProcessing.abs(newspos2 - spos2) > 1) {
			spos2 = spos2 + (newspos2-spos2)/loose;
		}
	}

	int constrain(int val, int minv, int maxv) {
		return Utils.globalProcessing.min(Utils.globalProcessing.max(val, minv), maxv);
	}

	boolean over1() {
		if(Utils.globalProcessing.mouseX > xpos && Utils.globalProcessing.mouseX < xpos+swidth &&
				Utils.globalProcessing.mouseY > ypos && Utils.globalProcessing.mouseY < ypos+sheight) {
			return true;
		} else {
			return false;
		}
	}
	boolean over2() {
		if(Utils.globalProcessing.mouseX > xpos && Utils.globalProcessing.mouseX < xpos+swidth &&
				Utils.globalProcessing.mouseY > ypos+sheight && Utils.globalProcessing.mouseY < ypos+2*sheight) {
			return true;
		} else {
			return false;
		}
	}

	void display() {
		if(!visible)
			return;
		
		Utils.globalProcessing.stroke(255);
		Utils.globalProcessing.rectMode(Utils.globalProcessing.CORNER);
		Utils.globalProcessing.fill(40);
		Utils.globalProcessing.rect(xpos, ypos, swidth, sheight);
		Utils.globalProcessing.rect(xpos, ypos+sheight, swidth, sheight);

		Utils.globalProcessing.fill(0);
		Utils.globalProcessing.stroke(0);
		Utils.globalProcessing.line(xpos+1, ypos+sheight/2, xpos+swidth-1, ypos+sheight/2);
		Utils.globalProcessing.line(xpos+1, ypos+3*sheight/2, xpos+swidth-1, ypos+3*sheight/2);
		Utils.globalProcessing.stroke(255);
		Utils.globalProcessing.line(138, ypos-5, 138, ypos+2*sheight);
		Utils.globalProcessing.line(250, ypos-5, 250, ypos+2*sheight);
		Utils.globalProcessing.line(384, ypos-5, 384, ypos+2*sheight);
		Utils.globalProcessing.line(489, ypos-5, 489, ypos+2*sheight);
		Utils.globalProcessing.line(585, ypos-5, 585, ypos+2*sheight);

		
		if(locked1) {
			Utils.globalProcessing.fill(153, 102, 0);
		} else {
			Utils.globalProcessing.fill(102, 102, 102);
		}
		Utils.globalProcessing.rect(spos1, ypos, sheight, sheight);
		if(locked2) {
			Utils.globalProcessing.fill(153, 102, 0);
		} else {
			Utils.globalProcessing.fill(102, 102, 102);
		}
		Utils.globalProcessing.rect(spos2, ypos+ sheight, sheight, sheight);
		float j;
		Episode ep;
		//System.out.println(newJslider1);
		if(newJslider1!=0){
			j=newJslider1;
			ep = Utils.episodes.get((int)(j+0.5));
			Utils.startEpisodeSelected = ep;
			Utils.endEpisodeSelected = ep;
		}
		else  {j = Utils.globalProcessing.map(getPos1(), 0, swidth-sheight/2-7, 0, Utils.episodes.size()-1);
		ep = Utils.episodes.get((int)(j+0.5));
		}
		//System.out.println(j);
		 
		if(Utils.startEpisodeSelected!=ep && !(Utils.globalProcessing.abs(newspos1 - spos1) > 1)){
		Utils.startEpisodeSelected = ep;
		//System.out.println("j= "+(int)(j+0.5)+" "+Utils.startEpisodeSelected);
		slider1Changed=true;
		}

		//System.out.println(getPos2());
		if(newJslider2!=0){
			j=newJslider2;
		}
		else j = Utils.globalProcessing.map(getPos2(), 0, swidth-sheight/2-7, 0, Utils.episodes.size()-1);
		//System.out.println(j);
		ep = Utils.episodes.get((int)(j+0.5));
		if(Utils.endEpisodeSelected!=ep && !(Utils.globalProcessing.abs(newspos2 - spos2) > 1) ){
		Utils.endEpisodeSelected = ep;
		//System.out.println("j= "+(int)(j+0.5)+" "+Utils.startEpisodeSelected);
		slider2Changed=true;
		}
		newJslider1=0;
		newJslider2=0;
		if(slider1Changed || slider2Changed)
			Utils.graphArea.createLineGraphPoints();

		slider1Changed=false;
		slider2Changed=false;
	}

	public void drawLowerPoninter(){
		Utils.globalProcessing.line(spos2, ypos, spos2, sheight/2);

	}

	float getPos1() {
		return (spos1-xpos);
	}
	float getPos2() {
		return (spos2-xpos) ;
	}


	public void mouseReleased() {	
	}


	public void modifySliders(Episode e) {
		//System.out.println("modificho gli sliders");
		
		// mvoe slider 1
		
		//newJslider1 =Utils.absoluteIndex(e);
		float j = Utils.globalProcessing.map(Utils.absoluteIndex(e), 0, Utils.episodes.size()-1,0, swidth-sheight/2-7);
		newspos1 = (int)(j+0.5)+xpos;
		sposMax1= (int)(newspos1);
		// move slider 2
		
		//newJslider2 =Utils.absoluteIndex(e);
		
		 j = Utils.globalProcessing.map(Utils.absoluteIndex(e), 0, Utils.episodes.size()-1,0, swidth-sheight/2-7);
		newspos2 = (int)(j+0.5)+xpos;
		sposMin2= (int)(newspos2);
		//System.out.println("j messo uguale a "+Utils.absoluteIndex(e));

		//newspos2 = j;
		//System.out.println("newspos2 = "+newspos2+"  sposMax1 = "+sposMax1);
		//sposMin2 =(int)(newspos2);
	}
}

